package jianshengaunli;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONArray;

/**
 * Servlet implementation class huiyuan
 */
@WebServlet(name="/huiyuan",urlPatterns="/huiyuan")
public class huiyuan extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public huiyuan() {
        super();
        // TODO Auto-generated constructor stub
    }

    
    
    public String shijain(){
    	
     LocalDateTime now = LocalDateTime.now();
        
        // 定义日期时间格式
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyyMMddHHmm");
        
        // 格式化日期时间
        String twelveDigitString = now.format(formatter);
        return twelveDigitString;
    }
    
    
    
    
    
    
	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
//		response.getWriter().append("Served at: ").append(request.getContextPath());
	
		
		response.setContentType("application/json;charset=UTF-8");
		request.setCharacterEncoding("UTF-8");
		response.setCharacterEncoding("utf-8");
		PrintWriter out = response.getWriter();

		String id = request.getParameter("id");
		String balance = request.getParameter("balance");
		System.out.println(id+"-"+"balance");
		//String dingdanh = request.getParameter("dingdanh");
		//String jinee = request.getParameter("jinee");
		//System.out.println(id + "-" + balance + "-" + dingdanh + "-" + jinee);
		Connection connection = null;

		PreparedStatement checkStmt = null;

		try {
				connection = DatabaseUtils.getConnection();
				if (connection != null) {
	        checkStmt = connection.prepareStatement("SELECT * FROM userinfo WHERE id = ?");
	        checkStmt.setString(1, id); // 
	        ResultSet rs = checkStmt.executeQuery();
	        PreparedStatement updateStmt = null;
	        if (rs.next()) {
	         
	            //System.out.println(id + "-" + balance);
	            updateStmt = connection.prepareStatement("UPDATE userinfo SET balance = balance + ?, totalprice = totalprice + ? WHERE id = ?");
	            updateStmt.setString(1, balance);
	            updateStmt.setString(2, balance);
	            updateStmt.setString(3, id);
	            int rowsAffected = updateStmt.executeUpdate();
	            if (rowsAffected > 0) {
	                PreparedStatement selectStmt = connection.prepareStatement("SELECT * FROM userinfo WHERE id = ?");
	                selectStmt.setString(1, id);
	                ResultSet rs2 = selectStmt.executeQuery();
	                JSONArray jsonArray = new JSONArray();
	                if (rs2.next()) {
	                    int id1 = rs2.getInt("id");
	                    String username = rs2.getString("username");
	                   
	                 
	                    String balance2 = rs2.getString("balance");
	                 
	                   

	                    JsonUtils.addToJsonArray(jsonArray, "id", id1, "username", username, "balance", balance2,"dingdanh", shijain(),"nihao",balance);
	                }
	                out.write(jsonArray.toString());
	            } else {
	                out.write("500");
	            }
	        } else {
	            out.write("500");
	        }
	    }

	} catch (Exception e) {
	    e.printStackTrace();
	    out.write("500");
	} finally {
	    DatabaseUtils.closeConnection(connection, checkStmt);
	}

		
	
	
	
	
	
	
	
	
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}

}
